<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page isELIgnored ="false" %>
<html> 
<head>
<meta http-equiv="Content-Type" content="text/jspl" charset="utf-8">
<%
String path = request.getContextPath();
pageContext.setAttribute("path",path);

pageContext.setAttribute("prefix","_addRole_");
%>
 
<script> 

function clearAddRoleFormData()
{
	document.getElementById("roleName").value="";
	document.getElementById("memo").value="";
	document.getElementById("nonLocked").checked=false;
	var nodes = document.getElementsByName("auth");
	for (var i=0;i<nodes.length ;++i )
	{
		nodes[i].checked=false
	}
}
function addRole()
{
		var data ={};
		data["name"]=document.getElementById("roleName").value;
		data["memo"]=document.getElementById("memo").value;
		data["nonLocked"]=document.getElementById("nonLocked").checked?1:0;
		data["deleteFlag"]=false;
		var fakeAuths=[];
		
		var nodes = document.getElementsByName("auth");
	
		var authsData="";
		for (var i=0;i<nodes.length ;++i )
		{
			fakeAuths.push({"id":nodes[i].value})
		}
		data["fakeAuths"]=fakeAuths;

		//var rest = new comba.component.Rest();
		HMSRest.AjaxPost("${path}/service/role",dojo.toJson(data),
			function(response,ioArgs){
			if (response.errorCode==0){
				alert("添加角色成功！");
				clearAddRoleFormData();
			}else{
				if (response.errorCode==1){
					alert("角色名称冲突,请选择其它的角色名称！");
					document.getElementById("roleName").value="";
				}
			}},
			function(error,ioArgs){
				alert(error);
			}
		 );

	return true; 
}

function getAllAuthInEditRole()
{
	HMSRest.AjaxGet("${path}/service/auths",function (response,ioArgs)
		{
			var cols = 3;
			var innerHtml ="<table id=\"tblAuths\"  border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
			var data = response.items;
			var i=0;
			for (;i<response.count;++i){
				if (i%cols == 0)
				{
					innerHtml += "<tr>";
				}
				innerHtml += "<td><input type='checkbox' name='auth' value=\""+data[i].id+"\" />&nbsp;" + data[i].name + "&nbsp;&nbsp;&nbsp;&nbsp; </td>";
				if (i%cols == cols-1)
				{
					innerHtml += "</tr>";
				}
			}
			var mod = i%cols;
			if (mod >0 )
			{
				for (i=0;i<mod-1 ;++i )
				{
					innerHtml += "<td>&nbsp;</td>";
				}
			}
			innerHtml += "</tr>";
			innerHtml +="</table>";
			document.getElementById("authsInAddRole").innerHTML=innerHtml;
		},
	function(response,ioArgs){});

}
</script> 
</head>
<body> 

<p>&nbsp;</P>
<div >
<form id="roleInfo"  action="/hms/service/role" method="POST"> 

<TABLE width="400px"  border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td align="left">基本信息:</td></tr>
<tr><td>
<TABLE   border="1" cellspacing="0" cellpadding="0" align="center">
<TR>
	<TD align="right">角色名称:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="name" id="roleName"/>
		<!-- <input type="button" value="检查重名" onclick="isExistSuchRoleName()"/></TD> -->
</TR>
<TR>
	<TD align="right">是否开锁:&nbsp;</TD><TD>&nbsp;<input type="checkbox" name="nonLocked" id="nonLocked" defaultChecked="true"/>  </TD>
</TR>
<TR>
	<TD  align="right">备注:&nbsp;</TD><TD>&nbsp;<input type="textfield" name="memo" size="32" id="memo"/></TD>
</TR>
</TABLE>
</td></tr>
<tr><td>授权选择:</td></tr>
<tr><td>
<div id="authsInAddRole" >
	
</div>
</td></tr>
<tr><td align="center"><input type="button" name="sub" value="保存" onclick="addRole();"/> </td></tr>
</table>

</form>
</div>
<script>
getAllAuthInEditRole();
</script> 
</body>
</html>
